package com.ruoyi.web.controller.cs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.cs.service.CommonService;
import com.ruoyi.cs.service.PicUploadService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 公共接口
 *
 * @author ruoyi
 * @date 2021-03-25
 */
@Api(tags = {"公共接口"})
@RestController
@RequestMapping("/cs/api")
class CommonApiController extends BasicsController {
    private static final Logger log = LoggerFactory.getLogger(CommonApiController.class);
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private PicUploadService picUploadService;

    @Value("${ruoyi.picpath}")
    private String picpath;
    /**
     * 获取参数列表
     * @param dictType  （goodsMold 武器种类）
     * @return
     */
    @PreAuthorize("@ss.hasPermi('cs:api:dictList')")
    @RequestMapping(name="获取参数列表",value="dictList",method = RequestMethod.GET)
    @ApiOperation("获取参数列表")
    @ApiImplicitParam(name = "dictType",value = "参数类型" ,dataType = "String", paramType = "query", required = true)
    public Response<List<SysDictData>> dictList(String  dictType)
    {
        List<SysDictData> list = dictDataService.dictList(dictType);
        return Response.success(list);
    }

    @ApiOperation("WEB获取参数列表")
    @RequestMapping(name="WEB获取参数列表",value="common/dictListweb",method = RequestMethod.GET)
    @ApiImplicitParam(name = "dictType",value = "字典类型" ,dataType = "String", paramType = "query", required = true)
    public Response<List<SysDictData>> dictListweb(String  dictType)
    {
        List<SysDictData> list = dictDataService.dictList(dictType);
        return Response.success(list);
    }

    /**
     * 根据武器种类获取饰品名称列表
     * @param goodsMold  （goodsMold 武器种类）
     * @return
     */
    @ApiOperation("根据武器种类获取饰品名称列表")
    @PreAuthorize("@ss.hasPermi('cs:api:goodsNameList')")
    @RequestMapping(name="根据武器种类获取饰品名称列表",value="goodsNameList",method = RequestMethod.GET)
    @ApiImplicitParam(name = "goodsMold",value = "字典类型" ,dataType = "String", paramType = "query", required = true)
    public Response<List<Map>> goodsNameList(String  goodsMold)
    {
        List<Map> list = commonService.goodsNameList(goodsMold);
        return Response.success(list);
    }

    /**
     * 箱子名称列表
     * @return
     */
    @ApiOperation("箱子名称列表")
    @PreAuthorize("@ss.hasPermi('cs:api:boxNameList')")
    @RequestMapping(name="箱子名称列表",value="boxNameList",method = RequestMethod.GET)
    public Response<List<Map>> boxNameList(){
        List<Map> list = commonService.boxNameList();
        return Response.success(list);
    }

    /**
     * ROLL房名称列表
     * @return
     */
    @ApiOperation("ROLL房名称列表")
    @PreAuthorize("@ss.hasPermi('cs:api:rollNameList')")
    @RequestMapping(name="ROLL房名称列表",value="rollNameList",method = RequestMethod.GET)
    public Response<List<Map>> rollNameList(){
        List<Map> list = commonService.rollNameList();
        return Response.success(list);
    }

    /**
     * 箱子下的饰品名称列表
     * @param goodsBelongingId
     * @return
     */
    @ApiOperation("箱子下的饰品名称列表")
    @PreAuthorize("@ss.hasPermi('cs:api:goodsNameListTerm')")
    @RequestMapping(name="箱子下的饰品名称列表",value="goodsNameListTerm",method = RequestMethod.GET)
    public Response<List<Map>> goodsNameListTerm(String  goodsBelongingId)
    {
        List<Map> list = commonService.goodsNameListAll(goodsBelongingId);
        return Response.success(list);
    }

    /**
     * 箱子标题列表
     * @param
     * @return
     */
    @ApiOperation("箱子标题列表")
    @PreAuthorize("@ss.hasPermi('cs:api:boxtitleNameList')")
    @RequestMapping(name="箱子标题列表",value="boxtitleNameList",method = RequestMethod.GET)
    public Response<List<Map>> boxtitleNameList(String titleType)
    {
        List<Map> list = commonService.boxtitleNameList(titleType);
        return Response.success(list);
    }

    /**
     * 后台图片上传
     * @param file
     * @return
     */
    @ApiOperation("后台图片上传")
    @RequestMapping(name="后台图片上传",value="avatarBack",method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('cs:api:avatarBack')")
    public  AjaxResult avatarBack(@RequestParam("avatarfile") MultipartFile file)  throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            if (loginUser == null) {
                return new AjaxResult(Constants.FAILCODE,"用户登录失效！");
            }
            String path = null;
            try {
                path = picUploadService.upload(file);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("上传图片异常，请联系管理员");
            }

//            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
//            String path = picpath+avatar.substring(RuoYiConfig.getProfile().length()+1);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", path);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 公告信息
     * @param
     * @return
     */
    @ApiOperation("公告信息")
    @RequestMapping(name="公告信息",value="common/notice",method = RequestMethod.GET)
    @Cacheable(value="twoMinuteCache", key="'notice'")
    public Response<String> notice()
    {
        SysDictData sysDictData = dictDataService.selectDictDataById(137L);
        return Response.success(sysDictData.getDictValue());
    }

    @ApiOperation("公告信息")
    @RequestMapping(name="公告信息",value="/notice",method = RequestMethod.GET)
    public Response<String> notice1()
    {
        SysDictData sysDictData = dictDataService.selectDictDataById(137L);
        return Response.success(sysDictData.getDictValue());
    }

    @ApiOperation("公告信息")
    @RequestMapping(name="公告信息",value="/noticeEdit",method = RequestMethod.GET)
    public Response<SysDictData> noticeEdit(String msg)
    {
        dictDataService.updateValue(msg,"137");
        return Response.success();
    }

    @ApiOperation("饰品价格接口")
    @RequestMapping(name="饰品价格接口",value="/common/price",method = RequestMethod.POST)
    public Response<String> price(@RequestBody List<String> marketHashNameList)
    {
        String result = commonService.priceInfo(marketHashNameList);
        JSONObject jsonObject = JSON.parseObject(result);
        String price = "0";
        if (jsonObject != null && jsonObject.getBoolean("success")) {
            JSONArray array = JSONObject.parseArray(jsonObject.getString("data"));
            if (array.size() > 0) {
                price = array.getJSONObject(0).getString("price");
            }
        }
        return Response.success(price);
    }

}
